<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Post;
use App\Models\Category;
use App\Models\Tag;
use Illuminate\Http\Request;
use Illuminate\View\View;

class PostController extends Controller
{
    /**
     * 显示文章列表页面
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request): View
    {
        $query = Post::with(['category', 'tags', 'user'])
            ->where('status', 'published')
            ->whereNotNull('published_at')
            ->where('published_at', '<=', now())
            ->orderBy('published_at', 'desc');

        // 分类筛选
        if ($request->has('category')) {
            $query->whereHas('category', function ($q) use ($request) {
                $q->where('slug', $request->category);
            });
        }

        // 标签筛选
        if ($request->has('tag')) {
            $query->whereHas('tags', function ($q) use ($request) {
                $q->where('slug', $request->tag);
            });
        }

        // 搜索
        if ($request->has('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%")
                  ->orWhere('excerpt', 'like', "%{$search}%")
                  ->orWhere('content', 'like', "%{$search}%");
            });
        }

        $posts = $query->paginate(12);
        
        // 获取分类及其文章数量
        $categories = Category::withCount(['posts' => function ($query) {
            $query->where('status', 'published')
                  ->whereNotNull('published_at')
                  ->where('published_at', '<=', now());
        }])->orderBy('name')->get();
        
        // 获取标签及其文章数量
        $tags = Tag::withCount(['posts' => function ($query) {
            $query->where('status', 'published')
                  ->whereNotNull('published_at')
                  ->where('published_at', '<=', now());
        }])->orderBy('name')->get();

        return view('posts.index', compact('posts', 'categories', 'tags'));
    }

    /**
     * 显示文章详情页面
     *
     * @param string $slug
     * @return View
     */
    public function show(string $slug): View
    {
        $post = Post::with(['category', 'tags', 'user'])
            ->where('slug', $slug)
            ->where('status', 'published')
            ->whereNotNull('published_at')
            ->where('published_at', '<=', now())
            ->firstOrFail();

        // 获取相关文章
        $relatedPosts = Post::with(['category', 'user'])
            ->where('status', 'published')
            ->whereNotNull('published_at')
            ->where('published_at', '<=', now())
            ->where('id', '!=', $post->id)
            ->where(function ($query) use ($post) {
                $query->where('category_id', $post->category_id)
                      ->orWhereHas('tags', function ($q) use ($post) {
                          $q->whereIn('tags.id', $post->tags->pluck('id'));
                      });
            })
            ->orderBy('published_at', 'desc')
            ->limit(4)
            ->get();

        return view('posts.show', compact('post', 'relatedPosts'));
    }
}